package com.acme;
import java.io.IOException;

import com.acme.bankapp.service.*;
import com.acme.bankapp.domain.*;
import com.acme.bankapp.exceptions.*;

public class BankApplication {
	public static void main(String[] args) throws NotEnoughFundsException, IOException {
		
		ClientRegistrationListener[] listeners = new ClientRegistrationListener[2];
		
		listeners[0] = new PrintClientListener();
		
		Bank bank = new Bank(listeners, 10);
		
		
					
		Client client1 = new Client("Vasya", Gender.FEMALE);
		Account acc1 = new SavingAccount(60);
		Email emToCli1 = new EmailImpl();
		emToCli1.setFrom("Me");
		emToCli1.setMessage("Email send");
		emToCli1.setSubject("subj");
		emToCli1.setTo("Vasya");
		EmailService.getEmailService().sendNotificationEmail(emToCli1);
		client1.addAccount(acc1);
		try {
			bank.addClient(client1);
		} catch (ClientExistsException e1) {
			System.out.println(e1.getMessage());
			return;
		}
		
		Client client2 = new Client("Vat", Gender.MALE);
		Account acc2 = new SavingAccount(20);
		Account acc3 = new CheckingAccount(40, 20);
		client2.addAccount(acc2);
		System.out.println("decimal value: "+acc2.decimalValue());
		client2.addAccount(acc3);
		try {
			bank.addClient(client2);
		} catch (ClientExistsException e1) {
			System.out.println(e1.getMessage());
			return;
		}
		
		BankService.printBalance(bank);
		try {
			BankService.modifyBank(bank, 20);
		} catch (OverDraftLimitExceededException e) {
			System.out.println("ERROR!");
			System.out.println(e.getMessage());
			return;
		}
		
		try {
			BankService.saveBank(bank, "saving.txt");
		} catch (SavingDataFailure e) {
			System.out.println("Saving Data Failure");
		}
		EmailService.getEmailService().sendNotificationEmail(emToCli1);
		BankService.printBalance(bank);
		BankService.printMaximumAmountToWithdraw(bank);
		EmailService.getEmailService().close();
	}
}
